<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
    <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.min.css" media="all" />
    <link rel="stylesheet" href="__CSS__/admin.css" media="all">
    <style type="text/css">
        #tooltip{
            position:absolute;border:1px solid#ccc;background:#333;padding:2px;display:none;color:#fff;
        }
    </style>
</head>
<body>
<fieldset class="layui-elem-field site-demo-button" style="margin-top: 30px;border:0">
    <form class="layui-form" action="{:url('admin/attachment/x')}" method="post" lay-filter="form">
        <div class="layui-form-item" style="float: left;">
            <div class="layui-input-inline">
                <input class="layui-input layui-btn-sm" type="text" name="keywords" autocomplete="off" placeholder="请输入关键词">
            </div>
            <div class="layui-input-inline">
                <div class="layui-inline">
                    <select name="status" lay-search="">
                        <option value="">状态</option>
                        <option value="0">待审核</option>
                        <option value="1">已审核</option>
                        <option value="-1">未通过</option>
                    </select>
                </div>
            </div>
            <div class="layui-input-inline">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input id="create_time" class="layui-input" type="text" name="create_time" placeholder="上传时间">
                    </div>
                </div>
            </div>
            <button class="layui-btn layui-btn-danger layui-btn-sm" lay-submit="" lay-filter="search">立即提交</button>
            <a type="button" class="layui-btn layui-btn-sm" id="test">上传压缩文件</a>
        </div>
    </form>
</fieldset>
<table id="table" class="layui-table" lay-filter="table"></table>
<script id="statusFormatter" type="text/html">
    {{#  if(d.status == -1){ }}
        <span class="layui-badge layui-bg-gray">未通过</span>
    {{#  } else if(d.status == 0){ }}
        <span class="layui-badge layui-bg-orange">待审核</span>
    {{# }else if(d.status == 1){ }}
    <span class="layui-badge">已审核</span>
    {{#  } }}
</script>
<script id="operate" type="text/html">
    <a class="layui-btn layui-btn-xs" style="margin-right: 0;font-size:12px;" lay-event="examine">审核</a>
    <a class="layui-btn layui-btn-xs" style="margin-right: 0;font-size:12px;" x="x" lay-event="download">下载</a>
    <button class="layui-btn layui-btn-xs layui-btn-danger"  style="margin-right: 0;font-size:12px;" lay-event="delete">删除</button>
</script>
<script src="__PUBLIC__/layui/layui.js" charset="utf-8"></script>
<script src="__PUBLIC__/jquery/jquery.min.js"></script>
<script>
var message;
layui.config({
    base: '__JS__/',
    version: '1.0.1'
}).use(['app','message'],function(){
    var app = layui.app,
        $ = layui.jquery,
        layer = layui.layer;
    //将message设置为全局以便子页面调用
    message = layui.message;
    //主入口
    app.set({
        type: 'iframe'
    }).init();
});
</script>
<script type="text/javascript">
$(function(){
    var x = 10;
    var y = 20;
    $(".tooltip").mouseover(function(e){
        var tooltip = "<div id='tooltip'><img src='" + this.href + "' alt='预览图' height='200'/>" + "<\/div>"; //创建 div 元素
        $("body").append(tooltip);  //把它追加到文档中             
        $("#tooltip")
            .css({
                "top":(e.pageY + y) + "px",
                "left":(e.pageX + x) + "px"
            }).show("fast");    //设置x坐标和y坐标，并且显示
    }).mouseout(function(){
        $("#tooltip").remove();  //移除 
    }).mousemove(function(e){
        $("#tooltip")
            .css({
                "top":(e.pageY + y) + "px",
                "left":(e.pageX + x) + "px"
            });
    });
});
</script>
<script>
layui.use('upload',function(){
    var $ = layui.jquery;
    var upload = layui.upload;
    //指定允许上传的文件类型
    upload.render({
        elem: '#test',
        url: "{:url('admin/attachment/upload')}",
        accept: 'file', //普通文件
        exts: 'zip|rar|7z', //只允许上传压缩文件
        done: function(res){
            layer.msg(res.msg);
            if(res.code == 1){
                setTimeout(function(){
                    location.href = res.url;
                },1500);
            }
        }
    });
});
</script>
<script>
layui.use(['laydate','form','table','layer'],function(){
    var laydate = layui.laydate;
    var  form 	= layui.form;
    var table 	= layui.table;
    var layer   = layui.layer;

    laydate.render({
        elem: '#create_time'
    });

    //搜索
    form.on('submit(search)', function(data){
        tableInit.reload({
            where:{serach:data.field}
        });
        return false;
    });

    var tableInit = table.render({
        id: 'table',
        elem: '#table',
        url: "{:url('admin/attachment/getlist')}",
        method: 'post',
        height: 600,
        page: true,
        cols: [[
            {checkbox:true,fixed:true,width:100},
            {field:'id',title:'编号',width:60},
            {field:'',title:'预览',width:60},
            {field:'module',title:'用途',width:100},
            {field:'filepath',title:'路径+名称',width:500},
            {field:'filesize',title:'大小',width:100},
            {field:'fileext',title:'格式',width:60},
            {field:'user_id',title:'上传id',width:80},
            {field:'uploadip',title:'上传IP',width:100},
            {field:'create_time',title:'上传时间',width:160},
            {field:'status',title:'状态',width:80,templet:'#statusFormatter'},
            {field:'admin_nickname',title:'审核者',width:80},
            {field:'audit_time',title:'审核时间',width:160},
            {field:'download',title:'已下载',width:100},
            {toolbar:'#operate',title:'操作',width:200,align:'center'}
        ]],
    });

    //监听工具条
    table.on('tool(table)', function(obj){
        var data = obj.data;
        var layEvent = obj.event; 
        var tr = obj.tr; 
        if(layEvent === 'examine'){
            var $id = data.id;
            layer.msg('文件审核',{
                time: 20000,
                btn: ['仁慈通过','残忍拒绝','再想想'],
                yes: function(index,layero){
                    $.ajax({
                        url: "{:url('admin/attachment/audit')}",
                        type: 'post',
                        data:{id:$id,status:'1'},
                        success: function(res){
                            layer.msg(res.msg);
                            if(res.code == 1){
                                setTimeout(function(){
                                    location.href = res.url;
                                },1500)
                            }
                        }
                    });
                },
                btn2: function(index,layero){
                    $.ajax({
                        url: "{:url('admin/attachment/audit')}",
                        type: 'post',
                        data:{id:$id,status:'-1'},
                        success: function(res){
                            layer.msg(res.msg);
                            if(res.code == 1){
                                setTimeout(function(){
                                    location.href = res.url;
                                },1500);
                            }
                        }
                    })
                }
            });
        }else if(layEvent === 'download'){
            var $id = data.id;
            var $this = $(this);
            $.ajax({
                url: "{:url('admin/attachment/download')}",
                data:{id:$id},
                success: function(res){                       
                    if(res.code == 1){
                        $this.attr('href',res.data);
                        $this.attr('download',res.name);
                    } else{
                        layer.msg(res.msg);
                    }
                }
            });
        }else if(layEvent === 'delete'){
            var $id = data.id;
            layer.confirm('确定要删除?',function(index){
                $.ajax({
                    url: "{:url('admin/attachment/delete')}",
                    data:{id:$id},
                    success: function(res){
                        layer.msg(res.msg);
                        if(res.code == 1){
                            setTimeout(function(){
                                location.href = res.url;
                            },1500);
                        }
                    }
                });
            });
        }
    });
    //批量删除
    $('#delAll').click(function(){
        layer.confirm('确认要删除选中信息吗？',{icon:3},function(index){
            layer.close(index);
            var checkStatus = table.checkStatus('table'); 
            $ids = $.map(checkStatus.data,function(o){
                return o.id;
            })
            if($ids.length == 0){
                layer.msg('未选中删除的管理员',{time:1000,icon:5});
            }else{
                var loading = layer.load(1,{shade:[0.1,'#fff']});
                $.post("{:url('admin/admin/delete')}", {id:$ids}, function(data){
                    layer.close(loading);
                    if(data.code === 0){
                        layer.msg(data.msg,{time:1000,icon:5});
                    }else{
                        layer.msg(data.msg, {time:1000,icon:6});
                    }
                    tableInit.reload();
                });
            }
        });
    });
});
</script>
</body>
</html>